package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.CustomException;
import com.demo.common.R;
import com.demo.dao.CategoryDao;
import com.demo.domain.Category;
import com.demo.domain.Dish;
import com.demo.domain.Setmeal;
import com.demo.service.CategoryService;
import com.demo.service.DishService;
import com.demo.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
     * 分页显示分页管理
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public R<Page> page(int page, int pageSize){
        //打印获取到前端传来的数据
        log.info("前端传来的page = {} pageSize = {} ", page, pageSize);

        //创建分页构造器
        Page<Category> pageInfo = new Page<>(page,pageSize);

        //创建查询构造器
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //添加排序条件
        lqw.orderByDesc(Category::getSort);

        //执行查询
        this.page(pageInfo,lqw);
        //传递给controller
        return R.success(pageInfo);
    }


    /**
     * 添加菜品类型与套餐
     * @param request
     * @param category
     * @return
     */
    @Override
    public R<String> save(HttpServletRequest request,@RequestBody Category category){

        this.save(category);

        return R.success("添加成功");
    }

    /**
     * 删除菜品类型与套餐
     * @param id
     * @return
     */
    @Override
    public R<String> deleteById(@PathVariable Long id){

        //查询菜品分类中与套餐是否有关联
        LambdaQueryWrapper<Setmeal> setmeallqw = new LambdaQueryWrapper<>();
        setmeallqw.eq(Setmeal::getCategoryId,id);
        int setmealCount = setmealService.count(setmeallqw);
        //如果有关联那么就抛出一个异常
        if(setmealCount>0){
            throw new CustomException("当前分类下关联了套餐，无法删除");
        }

        //查询菜品分类中与菜品是否有关联
        LambdaQueryWrapper<Dish> dishlqw = new LambdaQueryWrapper<>();
        dishlqw.eq(Dish::getCategoryId,id);
        int dishCount = dishService.count(dishlqw);
        //如果有关联那么就抛出一个异常
        if(dishCount>0){
            throw new CustomException("当前分类下关联了菜品，无法删除");
        }
       return R.error("删除失败");
    }

    /**
     * 修改菜品分类信息
     * @param category
     * @return
     */
    @Override
    public R<Category> updateCategory(@RequestBody Category category){
        this.updateById(category);
        return R.success(category);
    }

    /**
     * 菜品类型查询
     * @param category
     * @return
     */
    @Override
    public R<List<Category>> getDishList(Category category) {
        //查询type
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(category.getType() != null,Category::getType,category.getType());
       //添加排序条件
        lqw.orderByDesc(Category::getSort).orderByDesc(Category::getUpdateTime);

        List<Category> list = this.list(lqw);
        return R.success(list);
    }
}
